/**
 * 重写重定向方法，在重定向前加上fromUserName，重定向地址
 * @param url
 */
function redirect(url) {
	location.href = addFromUserName(url);
}

/**
 * 添加微信用户id，里面用到common.jsp里定义的fromUserName
 * @param url
 * @returns {String}
 */
function addFromUserName(url) {
	if (/.*\?.*fromUserName=.*/.test(url)) {
		return url.replace(/^(.*\?.*&?fromUserName=)[^&]*(&.*)*$/, "$1" + fromUserName + "$2");
	}
	return addParam(url, "fromUserName", fromUserName);
}

/**
 * 添加参数
 * @param url
 * @param paramKey
 * @param paramValue
 * @returns {String}
 */
function addParam(url, paramKey, paramValue) {
	if (!/^.*\?$/.test(url)) {
		if (url && url.indexOf("?") > -1) {
			url += "&";
		} else {
			url += "?";
		}
	}
	return url + paramKey + "=" + paramValue;
}

$(function(){
	if (!fromUserName && openacc && opencode) {
		$.ajax({
			url: "mvc/master/getOpenidByCode.htm",
			data: {
				openacc: openacc,
				code: opencode
			},
			success: function(data){
				if (data)
					fromUserName = data;
				location.href = redirect(location.href);
			}
		});
	}
});
